import { defineStore } from "pinia";
import { uGetStorageSync, uSetStorageSync } from "../utils/uniUtils";
import { reqPost } from "../common/http.request";
import { useShop } from "./useShop";

export const useUser = defineStore("user", {
  state: () => ({
    hasLogin: false,
    userInfo: {},
    shop_appid: 0,
    store: {},
    name: "hi,zhuli",
    age: 30,
    color: "#e15852", //全局颜色
    Btntitle: "立即购买", //
    couponNumber: 0,
    cardNumber: 0,
    nickName: "",
    phone: "",
  }),
  actions: {
    async login(scene) {
      uni.login({
        success: async (res) => {
          const accountInfo = uni.getAccountInfoSync();
          const { Data } = await reqPost("OnLogin", {
            appId: accountInfo.miniProgram.appId,
            code: res.code,
            scene: scene,
          });
          console.log("Data", Data);

          let Phone = Data.data[0].FPhone;
          let phoneID = Phone == null ? 0 : 1;
          uni.setStorageSync("mSystemAppID", Data.mSystemAppID);
          uni.setStorageSync("business_key", Data.business_key);
          uni.setStorageSync("PhoneID", phoneID);
          // uni.setStorageSync("Cookies", Data.cookies[0]);
          uni.setStorageSync("Members", {
            ...Data.memberdata,
            FPhone: Data.data[0].FPhone,
          }); //会员卡
          uni.setStorageSync("corpid", Data.stockdata[0].FCorpID); //公司id
          uni.setStorageSync("LoginID", 1);
          uni.setStorageSync("userInfo", {
            AvatarUrl: Data.data[0].FAvatarUrl,
            NickName: Data.memberdata[0].NiName,
            Number: Data.memberdata[0].Number,
            Phone: Data.data[0].FPhone,
            MemberID: Data.data[0].FMemberID,
          });

          if (Data.memberdata != null) {
            //会员卡资料

            uni.setStorageSync("stockdata", Data.stockdata[0]);
            if (Data.memberdata.length != 0) {
              uni.setStorageSync("memberdataID", 1);
              uni.setStorageSync("memberdata", Data.memberdata);
            } else {
              uni.setStorageSync("memberdataID", 0);
              uni.removeStorageSync("memberdata");
            }
          } else {
            uni.setStorageSync("memberdataID", 0);
            uni.removeStorageSync("memberdata");
          }
        },
      });
    },
    logout() {
      this.$reset();
      /*       uni.clearStorageSync("userInfo"); 
      uni.clearStorageSync("shop_appid");
      uni.clearStorageSync("hasLogin");
      uni.clearStorageSync("systemAppId");
      uni.clearStorageSync("store");
      uni.clearStorageSync("user");
      uni.clearStorageSync("stock");
      uni.clearStorageSync("member"); */
      uni.clearStorageSync();
    },
    async appLogin() {
      //获取当前帐号信息，可以返回小程序的Appid
      const accountInfo = uni.getAccountInfoSync();

      uni.login({
        success: async (res) => {
          const dt = await reqPost("OnUserLogin", {
            appId: accountInfo.miniProgram.appId,
            code: res.code,
            ...uGetStorageSync("m_store_info"),
          });

          const details = dt.Data[0];
          this.userInfo = details.userInfo;
          details &&
            Object.entries(details).forEach((item) => {
              uSetStorageSync(item[0], item[1]);
            });

          if (uGetStorageSync("hasLogin")) {
            this.hasLogin = true;
            useShop().GetShoppingCart();
          }
        },
        fail() {
          console.log("fail");
        },
      });
    },
  },
});
